package lmh.example.exam6_2;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        TextView textView = (TextView) findViewById(R.id.provider);   //获取显示最佳LocationProvider的TextView组件

        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
        //获取location服务
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        Criteria criteria=new Criteria();   //创建过滤条件
        criteria.setCostAllowed(false);    //使用不收费的
        criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);   //要求中等精度
        criteria.setPowerRequirement(Criteria.POWER_HIGH);  //要求高耗电量

        //获取最佳的LocationProvider名称
        String provider=locationManager.getBestProvider(criteria,true);
        textView.setText(provider);  //显示最佳的LocationProvider名称

    }
}
